package MDoc;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mx109
 */
class MgTableFactory {
    static PdfPTable getTable1(ArrayList<StringPair> L){
        int colNum=L.size();
        PdfPTable out = new PdfPTable(colNum+1);
        out.setWidthPercentage(100f);
        PdfPCell tmp = new PdfPCell(new Phrase(L.get(0).first, PolishFont.getFont(0, 14, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        tmp.setColspan(2);
        out.addCell(tmp);


        for (int i=1; i<colNum;++i){
            tmp=new PdfPCell(new Phrase(" "));
            tmp.setBorder(Rectangle.BOTTOM);
            out.addCell(tmp);
        }

        tmp = new PdfPCell(new Phrase(L.get(0).second, PolishFont.getFont(0, 14, Font.NORMAL)));
        setCenterCellAligment(tmp);
        tmp.setRowspan(3);
        tmp.setColspan(2);
        out.addCell(tmp);

        for (int i=1; i<colNum;++i){
            tmp = new PdfPCell(new Phrase(L.get(i).first, PolishFont.getFont(0, 12, Font.NORMAL)));
            tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
            setCenterCellAligment(tmp);
            out.addCell(tmp);
        }

        for (int i=1; i<colNum;++i){
            tmp = new PdfPCell(new Phrase(L.get(i).second, PolishFont.getFont(0, 12, Font.NORMAL)));
            setCenterCellAligment(tmp);
            tmp.setRowspan(2);
            out.addCell(tmp);
        }

        return out;

    }
    
    
    static PdfPTable getTable2(String a, String b, String c, String d){
        String podpis="\n\n\n....................................\nPodpis";
        PdfPCell tmp;
        PdfPTable out = new PdfPTable(3);
        ArrayList<String>L=new ArrayList<>();
        L.add(a);
        L.add(b);
        L.add(c);
        out.setWidthPercentage(100f);
        for (String s : L){
            tmp=new PdfPCell(new Phrase(s, PolishFont.getFont(12)));
            tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
            setCenterCellAligment(tmp);
            out.addCell(tmp);
        }
        for (int i=0; i<2; i++){
            tmp=new PdfPCell(new Paragraph(podpis, PolishFont.getFont(14)));
            tmp.setRowspan(2);
            setCenterCellAligment(tmp);
            out.addCell(tmp);
        }
        tmp=new PdfPCell(new Paragraph(d,PolishFont.getFont(12)));
        setCenterCellAligment(tmp);
        out.addCell(tmp);
        tmp=new PdfPCell();
        tmp.setBorder(Rectangle.LEFT | Rectangle.TOP);
        setCenterCellAligment(tmp);
        out.addCell(tmp);
        return out;
    }
    
    static PdfPTable getHeadTable(MgDoc mgdoc){
        PdfPTable mainTab = new PdfPTable(5);
        mainTab.setWidthPercentage(100f);
        PdfPCell tmp;
        
        tmp = new PdfPCell(new Phrase(mgdoc.DaneFirmy,PolishFont.getFont(0, 14, Font.ITALIC)));
        tmp.setColspan(2);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        
        tmp = new PdfPCell(new Phrase("\n"+mgdoc.Skrot+"\n\n",PolishFont.getFont(1, 30, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        
        tmp = new PdfPCell(new Phrase(mgdoc.Nazwa,PolishFont.getFont(1, 20, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        tmp.setColspan(2);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        
        
        tmp = new PdfPCell(new Paragraph(" "));
        tmp.setBorder(Rectangle.RIGHT | Rectangle.TOP);
        tmp.setColspan(2);
        tmp.setRowspan(2);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Paragraph("Data wystawienia:"));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Paragraph("Nr dokumentu:"));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Paragraph("Nr egzemlarza:"));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Paragraph(mgdoc.Data));
        tmp.setHorizontalAlignment(1);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Paragraph(mgdoc.NrDokumentu));
        tmp.setHorizontalAlignment(1);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Paragraph(mgdoc.NrEgzemplarza));
        tmp.setHorizontalAlignment(1);
        mainTab.addCell(tmp);
      return mainTab;
    
    }
    
    static PdfPTable getListTable(MgDoc mgdoc, MgList mgList){
        int width = 5;
        double sum = 0;
        if(mgList.showIlosc2){
            ++width;
        }
        if(mgList.showRoznicaIlosci){
            ++width;
        }
        if(mgList.koszty){
            width+=2;
        }
        PdfPTable mainTab = new PdfPTable(width);
        int[] relativeWidths= new int[width];
        if(mgList.koszty){
            relativeWidths[--width]=4;
            relativeWidths[--width]=4;
            sum+=8;
        }
        if(mgList.showRoznicaIlosci){
            relativeWidths[--width]=4;
            sum+=4;
        }
        relativeWidths[--width]=2;
        sum+=2;
        if(mgList.showIlosc2){
            relativeWidths[--width]=4;
            sum+=4;
        }
        relativeWidths[--width]=4;
        relativeWidths[--width]=4;
        relativeWidths[0]=1;
        sum+=9;
        relativeWidths[1]=26-(int)sum;
        if(relativeWidths[1]<6){
            relativeWidths[1]+=2;
            --relativeWidths[3];
            if(mgList.showIlosc2){
                --relativeWidths[4];
            }
        }
        mainTab.setWidthPercentage(100f);
        try {
            mainTab.setWidths(relativeWidths);
        } catch (DocumentException ex) {
            Logger.getLogger(MgTableFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        PdfPCell tmp;
        
        tmp = new PdfPCell(new Phrase("Lp", PolishFont.getFont(0, 12, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Phrase("Nazwa",PolishFont.getFont(0, 12, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Phrase("Sygnatura magazynowa",PolishFont.getFont(0, 12, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        tmp = new PdfPCell(new Phrase(mgList.IloscName,PolishFont.getFont(0, 12, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        if(mgList.showIlosc2){
            tmp = new PdfPCell(new Phrase(mgList.Ilosc2Name,PolishFont.getFont(0, 12, Font.NORMAL)));
            tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
            setCenterCellAligment(tmp);
            mainTab.addCell(tmp);
        }
        
        tmp = new PdfPCell(new Phrase("jm",PolishFont.getFont(0, 12, Font.NORMAL)));
        tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
        setCenterCellAligment(tmp);
        mainTab.addCell(tmp);
        
        if(mgList.showRoznicaIlosci){
            tmp = new PdfPCell(new Phrase(mgList.RoznicaIlosciName,PolishFont.getFont(0, 12, Font.NORMAL)));
            tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
            setCenterCellAligment(tmp);
            mainTab.addCell(tmp);
        }
        
        if(mgList.koszty){
            tmp = new PdfPCell(new Phrase("Koszt jednostkowy",PolishFont.getFont(0, 12, Font.NORMAL)));
            tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
            setCenterCellAligment(tmp);
            mainTab.addCell(tmp);
            
            tmp = new PdfPCell(new Phrase("Wartość",PolishFont.getFont(0, 12, Font.NORMAL)));
            tmp.setBackgroundColor(BaseColor.LIGHT_GRAY);
            setCenterCellAligment(tmp);
            mainTab.addCell(tmp);
        }
        
        sum=0;
        for(int i=0; i<mgList.List.size(); ++i){
            MgListObj produkt = mgList.List.get(i);
            
            tmp = new PdfPCell(new Paragraph((i+1)+"."));
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph(produkt.Nazwa,PolishFont.getFont(0, 12, Font.NORMAL)));
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph(produkt.SygnaturaMagazynowa,PolishFont.getFont(0, 12, Font.NORMAL)));
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph(doubleToStringFormat(produkt.Ilosc)));
            mainTab.addCell(tmp);

            if(mgList.showIlosc2){
                tmp = new PdfPCell(new Paragraph(doubleToStringFormat(produkt.Ilosc2)));
                mainTab.addCell(tmp);
            }
            tmp = new PdfPCell(new Paragraph(produkt.Jm));
            mainTab.addCell(tmp);

            if(mgList.showRoznicaIlosci){
                tmp = new PdfPCell(new Paragraph(doubleToStringFormat(produkt.Ilosc-produkt.Ilosc2)));
                mainTab.addCell(tmp);
            }

            if(mgList.koszty){
                tmp = new PdfPCell(new Paragraph(doubleToMoneyFormat(produkt.KosztJednostki)));
                mainTab.addCell(tmp);

                double x;
                if(mgList.showIlosc2){
                    x = produkt.KosztJednostki*produkt.Ilosc2;
                } else {
                    x = produkt.KosztJednostki*produkt.Ilosc;
                }
                tmp = new PdfPCell(new Paragraph(doubleToMoneyFormat(x)));
                sum+=x;
                mainTab.addCell(tmp);
            }
        }
        
        if(mgList.koszty){
            
            tmp = new PdfPCell(new Paragraph(" "));
            tmp.setBorder(Rectangle.RIGHT | Rectangle.TOP);
            tmp.setColspan(mainTab.getNumberOfColumns()-2);
            tmp.setRowspan(2);
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph("Koszt uboczny:"));
            tmp.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph(doubleToMoneyFormat(mgList.kosztUboczny)));
            sum+=mgList.kosztUboczny;
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph("Razem:"));
            tmp.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
            mainTab.addCell(tmp);

            tmp = new PdfPCell(new Paragraph(doubleToMoneyFormat(sum)));
            mainTab.addCell(tmp);
        }

      return mainTab;

    }
    
    private static void setCenterCellAligment(PdfPCell cell){
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    }
    
    private static String doubleToStringFormat(double x){
        if(x == (int) x){
            return String.format("%d",(int)x);
        } else {
            return String.format("%f", x).replaceAll("0*$", "");
        }
    }
    
    private static String doubleToMoneyFormat(double x){
        return String.format("%.2f", x+0.004);
    }
}



class PolishFont{
 static BaseFont getPolishTimesNewRomanBaseFont(){
     BaseFont bf=null;  
        try {
            bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1250, BaseFont.EMBEDDED);
        } catch (DocumentException | IOException ex) {
            Logger.getLogger(PolishFont.class.getName()).log(Level.SEVERE, null, ex);
        }
     return bf;
 }
 
 static BaseFont getPolishHelveticaRomanBaseFont(){
     BaseFont bf=null;  
        try {
            bf = BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1250, BaseFont.EMBEDDED);
        } catch (DocumentException | IOException ex) {
            Logger.getLogger(PolishFont.class.getName()).log(Level.SEVERE, null, ex);
        }
     return bf;
 }
  
 static Font getFont(int size){
     return getFont(0, size, Font.NORMAL);
 }
  
  static Font getFont(int type, int size, int style){
  if (type==0) {
          return new Font(getPolishHelveticaRomanBaseFont(), size, style);
      }
  return new Font(getPolishTimesNewRomanBaseFont(), size, style);
  }
             
}